import 'api_response.dart';

/// 设备信息模型
class Device {
  final int deviceId;
  final String deviceName;
  final String deviceSn;
  final int productId;
  final String productKey;
  final String productName;
  final int status;
  final String statusName;
  final String firmwareVersion;
  final String ipAddress;
  final String? lastOnlineTime;
  final String? lastOfflineTime;
  final double? latitude;
  final double? longitude;
  final String tags;
  final String createTime;
  final String? updateTime;
  final String tenantId;
  final String deviceImage;
  final String wvpDeviceId; // 国标监控设备ID

  Device({
    required this.deviceId,
    required this.deviceName,
    required this.deviceSn,
    required this.productId,
    required this.productKey,
    required this.productName,
    required this.status,
    required this.statusName,
    this.firmwareVersion = '',
    this.ipAddress = '',
    this.lastOnlineTime,
    this.lastOfflineTime,
    this.latitude,
    this.longitude,
    this.tags = '{}',
    required this.createTime,
    this.updateTime,
    required this.tenantId,
    this.deviceImage = '',
    this.wvpDeviceId = '', // 默认为空字符串
  });

  factory Device.fromJson(Map<String, dynamic> json) {
    return Device(
      deviceId: json['deviceId'],
      deviceName: json['deviceName'],
      deviceSn: json['deviceSn'],
      productId: json['productId'] ?? 0,
      productKey: json['productKey'] ?? '',
      productName: json['productName'] ?? '',
      status: json['status'],
      statusName: json['statusName'] ?? getStatusName(json['status']),
      firmwareVersion: json['firmwareVersion'] ?? '',
      ipAddress: json['ipAddress'] ?? '',
      lastOnlineTime: json['lastOnlineTime'],
      lastOfflineTime: json['lastOfflineTime'],
      latitude: json['latitude'] != null ? double.tryParse(json['latitude'].toString()) : null,
      longitude: json['longitude'] != null ? double.tryParse(json['longitude'].toString()) : null,
      tags: json['tags'] ?? '{}',
      createTime: json['createTime'] ?? '',
      updateTime: json['updateTime'],
      tenantId: json['tenantId'] ?? '',
      deviceImage: json['deviceImage'] ?? '',
      wvpDeviceId: json['wvpDeviceId'] ?? '', // 解析wvpDeviceId字段
    );
  }

  // 获取设备状态名称
  static String getStatusName(int status) {
    switch (status) {
      case 0:
        return '未激活';
      case 1:
        return '在线';
      case 2:
        return '离线';
      case 3:
        return '禁用';
      default:
        return '未知';
    }
  }
}

/// 设备列表响应
class DeviceListResponse {
  final int total;
  final List<Device> rows;
  final int code;
  final String msg;

  DeviceListResponse({
    required this.total,
    required this.rows,
    required this.code,
    required this.msg,
  });

  factory DeviceListResponse.fromJson(Map<String, dynamic> json) {
    return DeviceListResponse(
      total: json['total'],
      rows: (json['rows'] as List).map((e) => Device.fromJson(e)).toList(),
      code: json['code'],
      msg: json['msg'],
    );
  }

  bool get isSuccess => code == 200;
}

/// 设备详情响应
class DeviceDetailResponse extends ApiResponse<Device> {
  DeviceDetailResponse({
    required int code,
    required String msg,
    Device? data,
  }) : super(code: code, msg: msg, data: data);

  factory DeviceDetailResponse.fromJson(Map<String, dynamic> json) {
    return DeviceDetailResponse(
      code: json['code'],
      msg: json['msg'],
      data: json['data'] != null ? Device.fromJson(json['data']) : null,
    );
  }
}

/// 设备统计信息响应
class DeviceStatsResponse extends ApiResponse<DeviceStats> {
  DeviceStatsResponse({
    required int code,
    required String msg,
    DeviceStats? data,
  }) : super(code: code, msg: msg, data: data);

  factory DeviceStatsResponse.fromJson(Map<String, dynamic> json) {
    return DeviceStatsResponse(
      code: json['code'],
      msg: json['msg'],
      data: json['data'] != null ? DeviceStats.fromJson(json['data']) : null,
    );
  }
}

/// 设备统计信息
class DeviceStats {
  final int totalCount;
  final int onlineCount;
  final int offlineCount;
  final int inactiveCount;
  final int disabledCount;
  final int todayActivated;
  final int weekActivated;

  DeviceStats({
    required this.totalCount,
    required this.onlineCount,
    required this.offlineCount,
    required this.inactiveCount,
    required this.disabledCount,
    required this.todayActivated,
    required this.weekActivated,
  });

  factory DeviceStats.fromJson(Map<String, dynamic> json) {
    return DeviceStats(
      totalCount: json['totalCount'] ?? 0,
      onlineCount: json['onlineCount'] ?? 0,
      offlineCount: json['offlineCount'] ?? 0,
      inactiveCount: json['inactiveCount'] ?? 0,
      disabledCount: json['disabledCount'] ?? 0,
      todayActivated: json['todayActivated'] ?? 0,
      weekActivated: json['weekActivated'] ?? 0,
    );
  }
} 